from django.views import View
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from database import cm_conn
from uniforms.response import Response
import os
import pandas as pd

# Create your views here.


# 忽略 CSRF 保护
@csrf_exempt
# 限制 HTTP 方法为 GET
@require_http_methods(["GET"])
class ChaosMeshView(View):

    def get_ground_truth(request):
        if request.method == "GET":
            start_time = int(request.GET.get("start_time"))
            end_time = int(request.GET.get("end_time"))
            try:
                # raise Exception("An error occurred")
                ground_truth = cm_conn.create_ground_truth(start_time, end_time)
            except Exception as e:
                print(e)
                return Response.server_error(
                    message="Something goes wrong!", data={"ground_truth": {}}
                )
            if len(ground_truth) != 0:
                ground_truth = list(pd.DataFrame(ground_truth).T.to_dict().values())
            return Response.success(data={"ground_truth": ground_truth})
